package com.vinda.meetinglistviewdemo

import android.app.Activity
import android.os.Bundle
import android.util.Log
import android.view.View
import me.wxc.widget.ScheduleConfig
import me.wxc.widget.base.DailyTaskModel
import me.wxc.widget.base.ICalendarRender
import me.wxc.widget.base.IScheduleModel
import me.wxc.widget.schedule.ScheduleGroup
import me.wxc.widget.tools.adjustTimestamp
import me.wxc.widget.tools.calendar
import me.wxc.widget.tools.dayMillis
import me.wxc.widget.tools.hourMillis
import me.wxc.widget.tools.nowMillis
import me.wxc.widget.tools.yyyyMMddHHmmss
import java.util.Calendar

class MainActivity : Activity() {
    private val TAG: String = "MainActivity"
    var mScheduleGroup: ScheduleGroup? =null

    var dataList:List<IScheduleModel>  = listOf()
    lateinit var  childRenders: List<ICalendarRender>
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initializeScheduleConfig()
        setContentView(R.layout.activity_main)
        mScheduleGroup = findViewById(R.id.schedule_group)
        childRenders =   listOf(mScheduleGroup!!)

        //测试会议数据
        val newUserTasks = listOf(
            DailyTaskModel(
                id = System.currentTimeMillis(),
                beginTime = nowMillis.adjustTimestamp(
                    hourMillis, true
                ) - 2 * hourMillis, title = "新产品研讨会", duration = hourMillis
            ),
            DailyTaskModel(
                id = System.currentTimeMillis(),
                beginTime = nowMillis.adjustTimestamp(hourMillis, true).calendar.apply {
                    add(Calendar.DAY_OF_MONTH, 1)
                }.timeInMillis + 1 * hourMillis, title = "长按可滑动", duration = 2 * hourMillis
            ),
        )
        dataList = newUserTasks
        childRenders.onEach {
            it.reloadSchedulesFromProvider()
        }
    }


    private fun initializeScheduleConfig() {
        ScheduleConfig.run {
            scheduleBeginTime = nowMillis - 1 * dayMillis
            scheduleEndTime = nowMillis + 7 * dayMillis
            onDateSelectedListener = {
                                Log.i(TAG, "date select: $yyyyMMddHHmmss")
                //                binding.yyyyM.text = yyyyM
                //                selectedDayTime = timeInMillis
            }
            scheduleModelsProvider = { beginTime, endTime ->
                //获取
                dataList
            }
            //lifecycleScope = this.lifecycleScope
            onDailyTaskClickBlock = { model ->
                //                DetailsFragment().apply {
                //                    taskModel = model.copy()
                //                    onDeleteBlock = { ids ->
                //                        childRenders.onEach { it.reloadSchedulesFromProvider() }
                //                    }
                //                    onSaveBlock = {
                //                        Log.i(TAG, "daily task added: ${model.title}")
                //                        childRenders.onEach { it.reloadSchedulesFromProvider() }
                //                    }
                //                }.show(childFragmentManager, "DetailsFragment")
            }
            onCreateTaskClickBlock = { model ->
                //                if (binding.scheduleGroup.isVisible()) {
                //                    binding.scheduleGroup.children.filterIsInstance<IScheduleRender>().toList().onEach {
                //                        it.finishEditing()
                //                        (it as View).invalidate()
                //                    }
                //                }
                //                DetailsFragment().apply {
                //                    taskModel = model.copy()
                //                    onSaveBlock = {
                //                        Log.i(TAG, "daily task added: ${model.title}")
                //                        childRenders.onEach { it.reloadSchedulesFromProvider() }
                //                    }
                //                }.show(childFragmentManager, "DetailsFragment")

            }
            onTaskDraggedBlock = { model ->
                //lifecycleScope.launch {
                    //                    moreViewModel.updateSingleDailyTask(model)
                    //                    childRenders.onEach { it.reloadSchedulesFromProvider()
                //}
            }
        }

    }

    fun backToDay(view: View) {
//        childRenders.onEach {
//            it.reloadSchedulesFromProvider()
//        }
        mScheduleGroup?.selectedDayTime = nowMillis
    }
}
